package cn.com.ebidding.spc.controller;

import cn.com.ebidding.spc.common.CommonResult;
import cn.com.ebidding.spc.common.PaymentInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created on 2020/9/17.
 *
 * @author sehuaqi.
 * 描述：订单服务接口
 */
@RestController
@Slf4j
public class OrderController {

    public static final String SPC_PROVIDER = "SPC-PROVIDER-SERVICE";

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/get/{id}")
    public String getPayment(@PathVariable("id") Long id) {
        log.info("进入订单服务接口，调用支付服务接口");

        List<ServiceInstance> instances = discoveryClient.getInstances(SPC_PROVIDER);

        if(instances == null || instances.size() <= 0){
            return null;
        }

        String result = restTemplate.getForObject(instances.get(0).getUri() + "/payment/get/" + id, String.class);

        log.info("调用支付服务接口，获取到的数据 = {}", result);

        return result;
    }

    @GetMapping("/consumer/lb/payment/get/{id}")
    public CommonResult getPaymentRibbon(@PathVariable("id") Long id) {
        log.info("进入订单服务接口，调用支付服务接口");

        CommonResult<PaymentInfoVo> paymentInfoVo = restTemplate.getForObject("http://" + SPC_PROVIDER + "/payment/get/" + id, CommonResult.class);

        log.info("调用支付服务接口，获取到的数据 = {}", paymentInfoVo);

        return paymentInfoVo;
    }

}
